#!/usr/bin/perl
# Display a list of other nas devices

require './mdmsearch-lib.pl';
&header($text{'index_title'}, "", "index", 0, 1);
if (!(-d $config{'searchdir'})){
	`mkdir -p $config{'searchdir'}`;
}

if (!(-f $config{'mdmlogfile'})){
	`mkdir -p $config{'mdmlogdir'}`;
	`touch $config{'mdmlogfile'}`;
}

&foreign_require("mdmmod","mdmmod-lib.pl");
local $mdmmod=&foreign_call("mdmmod","get_mdm_mod");
my @servers = sort { $a->{'id'} cmp $b->{'id'} } &list_servers;

if (@servers) {
	print "<table class=mode1 border=0 cellpadding=4 cellspacing=1 width=100%>\n";
	if ($mdmmod eq 'server'){
		print "<tr class=title1>\n";
		print "<td><b>$text{'index_id'}</b></td>\n";
		print "<td><b>$text{'index_hostname'}</b></td>\n";
		print "<td><b>$text{'index_ip'}</b></td>\n";
		print "<td><b>$text{'index_control'}</b></td>\n";	
		print "<td><b>$text{'index_group'}</b></td>\n";
		print "<td><b>$text{'index_status'} ($text{'index_statusmsg'})</b></td> </tr>\n";
		foreach $s (@servers) {
			if ($mode eq 'mode1'){
				$mode='mode2';
			}else{
				$mode='mode1';
			}
			print "<tr class=$mode>\n";
			print "<td>$s->{'id'}</td>\n";
			print "<td>$s->{'hostname'}</td>\n";	
			print "<td><a href=".&make_url($s).">\n";		
			print "$s->{'ip'}</a></td>\n";
			printf "<td><table border=0 cellpadding=0 cellspacing=0 width=100% ><tr>\n",$mode;
			print "<td>",$s->{'user'}?$text{'stat_control'}:$text{'stat_notcontrol'},"</td>\n";
			print "<td align=right>";
			print "<a href='edit_serv.cgi?id=$s->{'id'}'><front size=1>[$text{'index_edit'}]</front></a>\n";
			print "</td> </tr></table></td>\n";
			print "<td>",$s->{'group'}?$s->{'group'}:$text{'index_none'},"</td>\n";
			print "<td>",$s->{'status'}?$text{'stat_run'}:$text{'stat_notrun'},"</td></tr>\n";
		}
	}else{
		print "<tr class=title1>\n";
		print "<td><b>$text{'index_id'}</b></td>\n";
		print "<td><b>$text{'index_hostname'}</b></td>\n";
		print "<td><b>$text{'index_ip'}</b></td>\n";
		print "<td><b>$text{'index_status'} ($text{'index_statusmsg'})</b></td> </tr>\n";
		foreach $s (@servers) {
			if ($mode eq 'mode1'){
				$mode='mode2';
			}else{
				$mode='mode1';
			}
			print "<tr class=$mode>\n";
			print "<td>$s->{'id'}</td>\n";
			print "<td>$s->{'hostname'}</td>\n";	
			if ($s->{'user'}) {
				print "<td><a href='link.cgi/$s->{'id'}/'>\n";
			}else {
				print "<td><a href=".&make_url($s).">\n";
			}
		
			print "$s->{'ip'}</a></td>\n";
			print "<td>",$s->{'status'}?$text{'stat_run'}:$text{'stat_notrun'},"</td></tr>\n";
		}
	}
	print "</table>\n";
}else {
	print "$text{'index_noservers'}\n";
}

print "</table>\n";
print "<a href='edit_serv.cgi?new=1'>$text{'index_add'}</a> <p>\n" if $access{'edit'};

print "<p><hr>\n";
print "<table width=100%>\n";
print "<form action=find_serv.cgi>\n";
print "<tr> <td><input type=submit class=button value=\"$text{'index_regist'}\">\n";
print "$text{'index_registmsg'}</td></tr>\n";
print "<input type=hidden name=findtype value='S'>\n";
print "<input type=hidden name=register value='write'>\n";
print "</form>\n";
print "<form action=find_serv.cgi>\n";
print "<tr> <td><input type=submit class=button value=\"$text{'index_multicast'}\">\n";
print "$text{'index_multimsg'}</td></tr>\n";
print "<input type=hidden name=findtype value='M'>\n";
print "</form>\n";
print "<form action=find_serv.cgi>\n";
print "<tr> <td><input type=submit class=button value=\"$text{'index_broad'}\">\n";
print "$text{'index_broadmsg'}</td></tr>\n";
print "<input type=hidden name=findtype value='B'>\n";
print "</form>\n";
print "</table>\n";

&footer("/", $text{'index'});

sub make_url
{
return sprintf "http%s://%s:%d/",
	$_[0]->{'ssl'} ? 's' : '', $_[0]->{'ip'}, $_[0]->{'port'};
}

1;